﻿using System.Windows;
using System.Windows.Controls;

namespace VS.Infrastructure.Controls
{
    public class TagCloud : ItemsControl
    {
        public static readonly DependencyProperty ItemContainerStyleProperty =
         DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(TagCloud), null);

        public Style ItemContainerStyle
        {
            get { return (Style)GetValue(ItemContainerStyleProperty); }
            set { SetValue(ItemContainerStyleProperty, value); }
        }

        public TagCloud()
            : base()
        {
            DefaultStyleKey = typeof(TagCloud);
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return (item is TagCloudItem);
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TagCloudItem();
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            TagCloudItem tci = element as TagCloudItem;
            if (ItemContainerStyle != null && tci != null && tci.Style == null)
            {
                tci.SetValue(Control.StyleProperty, this.ItemContainerStyle);
            }

            base.PrepareContainerForItemOverride(element, item);
        }

    }
}
